package com.smarthome.web.property.controller.housemanager;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.housemanager.BuildingManagerService;
import com.smarthome.core.housemanager.ResidenceManagerService;
import com.smarthome.domain.bean.ShBuilding;
import com.smarthome.domain.bean.ShResidence;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.web.property.utils.UserHolder;

/**
 * @Title 楼栋管理
 * @author dhc
 *
 */
@Controller()
@RequestMapping("/buildingManager")
public class BuildingManagerController {
	
	private static Logger logger = LoggerFactory.getLogger(BuildingManagerController.class);
	
	@Resource(name="buildingManagerService")
	private BuildingManagerService buildingManagerService;
	@Resource(name="residenceManagerService")
	private ResidenceManagerService residenceManagerService;
	
	private  boolean isAdmin= true;
	@RequestMapping("views")
	public String  buildingManager(HttpServletRequest request){
		isAdmin = UserHolder.isSuperResidence();
		
		request.setAttribute("isAdmin",isAdmin);
		
		if(isAdmin){
			request.setAttribute("residentces",residenceManagerService.queryShResidences(null));
		} else {
			Long residenceId = UserHolder.getCurrentResidenceId() ;
			ShResidence residence = new ShResidence();
			residence.setId(residenceId);
			//设置当前小区
			request.setAttribute("residence", residence);
			request.setAttribute("building", buildingManagerService.queryShBuildingsByResidenceId(residenceId));
		}
		
		return "/housemanager/buildingManager";
	}
	
	@RequestMapping("/combobox")
    @ResponseBody
	public List<Combobox> getCombobox(Long residenceId){
		return buildingManagerService.queryComboboxByResidenceId(residenceId);
	}
	
	@RequestMapping("/dataGrid")
    @ResponseBody
	public DataGrid getPage(ShBuilding building, PageHelper page){
		
		return buildingManagerService.getDataGrid(building, page);
	}
	@RequestMapping("/addPage")
    public String addPage(HttpServletRequest request,Long residenceId) {
		isAdmin = UserHolder.isSuperResidence();
		request.setAttribute("isAdmin", isAdmin);
		/**获取小区域*/
		//判断权限  
		if(isAdmin){
			request.setAttribute("residentces",residenceManagerService.queryShResidences(null));
		} else{
			//设置当前所在的小区
			residenceId = UserHolder.getCurrentResidenceId();
			ShResidence residence = new ShResidence();
			residence.setId(residenceId);
			//设置当前小区
			request.setAttribute("residence", 	residenceManagerService.queryResidenceById(residenceId));
		}
		
		
        return "/housemanager/buildingAdd";
	}
	@RequestMapping("/editPage")
	public String eidtPage(HttpServletRequest request,Long id) {
		
		request.setAttribute("building", buildingManagerService.querybuildingById(id));
		
		return "/housemanager/buildingEdit";
	}
	
	@ResponseBody
	@RequestMapping("/add")
	public Result add(ShBuilding building){
		
		if(StringUtils.isBlank(building.getName())){
			return Result.failure("名字不能为空");
		}
		
		//重名验证
		if(buildingManagerService.isRepeat(building)){
			return Result.failure("楼栋名称重复");
		}
		
		if(building.getResidenceId() == null){
			return Result.failure("必须关联一个小区");
		}
		
		buildingManagerService.add(building);
		return Result.success("添加房屋楼栋成功");
	}
	
	@ResponseBody
	@RequestMapping("/edit")
	public Result edit(ShBuilding building){
		if(StringUtils.isBlank(building.getName())){
			return Result.failure("名字不能为空");
		}
		
		//重名验证
		if(buildingManagerService.isRepeat(building)){
			return Result.failure("楼栋名称重复");
		}
		
		if(building.getResidenceId() == null){
			return Result.failure("请选择一个小区进行添加");
		}
		
		buildingManagerService.edit(building);
		return Result.success("修改房屋楼栋成功");
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(HttpServletRequest request,Long id){
		buildingManagerService.delte(id);
		return Result.success("删除房屋楼栋成功");
	}
	
}
